package com.zx.dsptuneup_TriMic.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.base.tools.tools.StringUtils;
import com.zx.dsptuneup_TriMic.Base.BaseAdapter;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.bean.DeviceBean;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
 * @author cyj
 * @createDate 2021-11-15
 * @ClassName: TransferChildAdapter
 * @Description: 转移子设备
 * @Version: 1.0
 */
public class TransferChildAdapter extends BaseAdapter {
    private Context context;
    private List<DeviceBean> gridViewBeanList;

    private int currentSelIndex = -1;     // 当前单选的选中项

    public List<DeviceBean> getGridViewBeanList() {
        return gridViewBeanList;
    }
    public void setGridViewBeanList(List<DeviceBean> gridViewBeanList) {
        this.gridViewBeanList = gridViewBeanList;
        currentSelIndex = -1;
        notifyDataSetChanged();
    }

    public int getCurrentSelIndex() {
        return currentSelIndex;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.parrent_item_transfer,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ViewHolder groupViewHolder = (ViewHolder) holder;
        DeviceBean deviceBean = gridViewBeanList.get(position);
        String devicesInfo = StringUtils.isEmpty(deviceBean.getDeviceName()) ? "未知设备名" : deviceBean.getDeviceName();
        groupViewHolder.main_title.setText(devicesInfo);
        groupViewHolder.tab_tag.setText((position+1)+".");
        groupViewHolder.sub_title.setText(StringUtils.isEmpty(deviceBean.getPositionName()) ? "未知位置" : deviceBean.getPositionName());

        groupViewHolder.group_title.setText(" (编组:"+ deviceBean.getGroup() + ")");

        // status 1 正常 2 忙碌 3 异常 4 掉线
        GradientDrawable gd = (GradientDrawable)groupViewHolder.ststus_circle.getBackground();
        switch (deviceBean.getStatus()){
            case 1:
                groupViewHolder.equip_status.setText("已上线");
                gd.setColor(Color.parseColor("#5BCB00"));
                break;
            case 2:
                groupViewHolder.equip_status.setText("忙碌");
                gd.setColor(Color.parseColor("#486FFF"));
                break;
            case 3:
                groupViewHolder.equip_status.setText("异常");
                gd.setColor(Color.parseColor("#FEAA00"));
                break;
            case 4:
                groupViewHolder.equip_status.setText("掉线");
                gd.setColor(Color.parseColor("#D2D2D2"));
                break;
        }
        groupViewHolder.sel_check.setSelected(position ==currentSelIndex);
        groupViewHolder.bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(position == currentSelIndex){
                    return;
                }
                currentSelIndex = position;
                notifyDataSetChanged();
            }
        });
    }


    @Override
    public int getItemCount() {
        return gridViewBeanList.size();
    }

    public TransferChildAdapter(Context context, List<DeviceBean> gridViewBeanList) {
        super(context);
        this.context = context;
        this.gridViewBeanList = gridViewBeanList;
    }

    @Override
    public void notifyDataSetChanged(List dataList) {

    }


    @Override
    public long getItemId(int i) {
        return i;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tab_tag)
        TextView tab_tag;
        @BindView(R.id.main_title)
        TextView main_title;
        @BindView(R.id.sub_title)
        TextView sub_title;
        @BindView(R.id.group_title)
        TextView group_title;
        @BindView(R.id.equip_status)
        TextView equip_status;
        @BindView(R.id.ststus_circle)
        TextView ststus_circle;
        @BindView(R.id.sel_check)
        TextView sel_check;
        @BindView(R.id.hsv)
        RelativeLayout bg;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
